#pragma once
#include <iostream>
#include <string>
#include "myh/socket.hpp"
#include "myh/MyLog.hpp"
#include "myh/pthreadpool.hpp"
#include <memory>
#include <mutex>
extern MyLog lg;
class udpclient
{
    typedef void (*task)();
    typedef std::shared_ptr<udpclient> UdpPtr;
    typedef std::shared_ptr<Socket> SocketPtr;
    static void funrun()
    {
        std::string str;
        while(true)
        {
            std::cout <<"输入查询单词:";
            std::getline(std::cin,str);
            _this->_csocket->Sendto(str);
            str.clear();
        }
       
    }

public:
    void run()
    {
        PthPool<task>::Getinitance(1)->start();
        PthPool<task>::Getinitance()->push(funrun);
        sockaddr_in serveaddr; 
        std::string s;
        while(true)
        {
            s = _this->_csocket->Recvform(&serveaddr);
            std::cout << "意思是:" << s <<std::endl;
        }
    }

    static UdpPtr Getinitance(uint16_t port = 0, const std::string &ip = "", int domain = AF_INET)
    {
        static std::once_flag once;
        std::call_once(once, [port, ip, domain]()
                       { _this = UdpPtr(new udpclient(port, ip, domain)); });
        return _this;
    }

private:
    udpclient(const udpclient& u) = delete;
    udpclient& operator=(const udpclient& u) = delete;
    udpclient(uint16_t port, const std::string &ip, int domain = AF_INET)
        : _csocket(SocketPtr(new Socket(port, ip, domain,SOCK_DGRAM)))
    {
    }

private:
    SocketPtr _csocket;
    static UdpPtr _this;
};

udpclient::UdpPtr udpclient::_this = nullptr;